import config from "@/config"; // 导入配置文件

const baseURL = config.baseURL;
import MyStore from '@/store/index.js'


// 任务相关请求
export const taskAPI = {
    // 获取“可接单”列表数据
    getOrderList(maintainerId) {
        return new Promise((resolve, reject) => {
			const token = uni.getStorageSync('token');
            uni.showLoading({
                title: '加载中'
            });
            uni.request({
                url: `${baseURL}/order/1`,
                method: "POST",
                data: {
                    "maintainerId": maintainerId
                },
                header: {
                    "token": token
                },
                success(res) {
                    if (res.data.code === 200) {
                        const orderListData = res.data.data.records.filter(item => item.stateName === "分派中");
                        resolve(orderListData);
                    } else {
                        reject("查询失败");
                    }
                },
                fail(err) {
                    reject("服务器罢工了");
					uni.showToast({
						title: "服务器罢工了",
						image: '/static/image/icon/Disconnected.png',
						duration: 2000
					})
                },
                complete() {
                    uni.hideLoading();
                }
            });
        });
    },

    // 获取“进行中”列表数据
    getInProgress(maintainerId) {
        return new Promise((resolve, reject) => {
			const token = uni.getStorageSync('token');
            uni.showLoading({
                title: '加载中'
            });
            uni.request({
                url: `${baseURL}/order/1`,
                method: "POST",
                data: {
                    "maintainerId": maintainerId
                },
                header: {
                    "token": token
                },
                success(res) {
                    if (res.data.code === 200) {
                        const inProgressData = res.data.data.records.filter(item => {
                            const stateName = item.stateName;
                            return stateName === "检查中" || stateName === "维修中" || stateName === "选件中";
                        });
                        resolve(inProgressData);
                    } else {
                        reject("查询失败");
                    }
                },
                fail(err) {
                    reject("服务器罢工了");
					uni.showToast({
						title: "服务器罢工了",
						image: '/static/image/icon/Disconnected.png',
						duration: 2000
					})
                },
                complete() {
                    uni.hideLoading();
                }
            });
        });
    },

    // 确认接单
    acceptOrder(orderId) {
        return new Promise((resolve, reject) => {
			const token = uni.getStorageSync('token');
            uni.request({
                url: `${baseURL}/order/next`,
                method: 'POST',
                data: {
                    "id": orderId,
                },
                header: {
                    "token": token
                },
                success(res) {
                    if (res.data.code === 200) {
                        resolve();
                    } else {
                        reject("确认接单失败");
                    }
                },
                fail(err) {
                    reject("服务器罢工了");
					uni.showToast({
						title: "服务器罢工了",
						image: '/static/image/icon/Disconnected.png',
						duration: 2000
					})
                }
            });
        });
    },

    // 拒绝接单
    refuseOrder(orderId) {
        return new Promise((resolve, reject) => {
			const token = uni.getStorageSync('token');
            uni.request({
                url: `${baseURL}/order/rollback/${orderId}`,
                method: 'POST',
                header: {
                    "token": token
                },
                success(res) {
                    if (res.data.code === 200) {
                        resolve();
                    } else {
                        reject("拒绝接单失败");
                    }
                },
                fail(err) {
                    reject("服务器罢工了");
					uni.showToast({
						title: "服务器罢工了",
						image: '/static/image/icon/Disconnected.png',
						duration: 2000
					})
                }
            });
        });
    },

    // 查看详情
    viewOrderDetails(orderId, taskState, machineModelNumber) {
        // 存储点击“查看详情”工单的ID和工单中的设备型号
        MyStore.dispatch('setClickOrderStailsIdAsync', orderId);
        MyStore.dispatch('setClickOrderStailsmachineModelNumberAsync', machineModelNumber);

        if (taskState === "检查中") {
            uni.navigateTo({
                url: '/pages/task-check-details/task-check-details'
            });
        } else if (taskState === "选件中") {
            uni.navigateTo({
                url: '/pages/task-option-details/task-option-details'
            });
        } else if (taskState === "维修中") {
            uni.navigateTo({
                url: '/pages/task-repair-details/task-repair-details'
            });
        }
    }
};

// “我的”相关请求
export const myAPI = {
    // 获取维修员信息
    getMaintainerInfo(userId) {
        return new Promise((resolve, reject) => {
            const userData = uni.getStorageSync('userData');
            if (userData == "") {
                reject("未登录");
                return;
            } else if (userData.role != 'employee') {
                reject("不是维修员");
                return;
            }
            
            uni.showLoading({
                title: '加载中'
            });
            uni.request({
                url: `${baseURL}/maintainer/${userId}`,
                method: "GET",
                header: {
                    "token": token
                },
                success(res) {
                    if (res.data.code === 200) {
                        const maintainerInfo = {
                            avatar: userData.user.avatar,
                            nickName: userData.user.username,
                            state: getMaintainerState(userData.user.stateEnum),
                            maintainerId: userData.user.id
                        };
                        resolve(maintainerInfo);
                    } else {
                        reject("获取维修员信息失败");
                    }
                },
                fail(err) {
                    reject("服务器罢工了");
					uni.showToast({
						title: "服务器罢工了",
						image: '/static/image/icon/Disconnected.png',
						duration: 2000
					})
                },
                complete() {
                    uni.hideLoading();
                }
            });
        });
    }
};

// 获取维修员状态文字
function getMaintainerState(stateEnum) {
    switch (stateEnum) {
        case "ON":
            return "已上班";
        case "VACATION":
            return "休假中";
        case "WORKING":
            return "任务中";
        case "OFF":
            return "已下班";
        default:
            return "";
    }
}
